/**
 * 循环队列——击鼓传花
 * 围成一个圆 
 * a -> b -> c
 * ↑         ↓
 * f <- e <- d
 */

/**
 * 击鼓传花
 * @param  {Array} nameList 孩子列表
 * @param  {Number} num      传花次数
 * @return {[type]}          [description]
 */
function hotPotato(nameList, num) {
  var queue = new Queue();

  for(var i = 0; i < nameList.length; i++){
    queue.enqueue(nameList[i]);
  }

  var eliminated = '';
  while(queue.size() > 1) {
    for(var i = 0; i < num; i++){
      queue.enqueue(queue.dequeue());
    }
    eliminated = queue.dequeue();
    console.log(`${eliminated}被淘汰了`);
  }
  console.log(`胜利的是${queue.dequeue()}`)
}

function Queue(){
  var items = [];

  this.enqueue = function(element){
    items.push(element);
  }

  this.dequeue = function(){
    return items.shift();
  }

  this.front = function(){
    return items[0];
  }

  this.isEmpty = function(){
    return items.length === 0;
  }

  this.clear = function(){
    items = [];
  }

  this.size = function(){
    return items.length;
  }

}